﻿@using OSBLE.Models
@using OSBLE.Models.Courses
@using OSBLE.Models.HomePage



<script type="text/javascript">

    function RemainingChars() {
        var remaining =  $('#MaxContentChars').val() - $('#ActivityFeed').val().length;
        $('#remChars').val("(" + remaining + ") Characters left");
    }

</script>

<div id="activity_new_post">
    @if (ViewBag.ActiveCourse.AbstractRole != null)
    {
    if ((ViewBag.ActiveCourse.AbstractRole.CanGrade == true) || (ViewBag.ActiveCourse.AbstractRole.Anonymized) || ((ViewBag.ActiveCourse.AbstractCourse is Course) && (ViewBag.ActiveCourse.AbstractCourse.AllowDashboardPosts) == true))
    {
        <input type="hidden" id="MaxContentChars" value="@ViewBag.MaxActivityFeedLength"/>
        
        if (ViewBag.Error != null)
        {
        <h3 class="error">@ViewBag.Error</h3>
        }
        using (Html.BeginForm("NewPost", "Home", FormMethod.Post))
        {
        <input id="remChars" name="remainingChars" readonly="true" type="text" value="(@ViewBag.MaxActivityFeedLength) Characters left" /><br />
        <textarea id="ActivityFeed" name="Content" rows="5" cols="40" placeholder="Enter new feed post here..." 
        onkeyup="RemainingChars();" onkeydown="RemainingChars();" ></textarea><br />
        
        <input type="submit" name="post_active" value="Post to @Helpers.CourseTag(ViewBag.ActiveCourse.AbstractCourse)" /> 
        
            if (ViewBag.IsInstructor)
            {
                <input type="submit" name="post_all" value="Post to All Courses I Teach" onclick="return confirm('Post this message to all your taught courses?');" />
                <input type="checkbox" value="True" name="send_email" /> <text>Email to Class</text>
            }

            if (ViewBag.IsLeader)
            {
                <input type="checkbox" value="True" name="send_email" /> <text>Email to Community</text>
            }

        }
    }
    }
</div>
<div class="dashboard_mode">
    @using (Html.BeginForm("SetDashboardMode", "Home", FormMethod.Post, new { id = "set_dashboard" }))
    {
        <input type="radio" name="mode" value="false" @if (!ViewBag.DashboardSingleCourseMode) {<text>checked="checked"</text>}/>
            <text>Show all courses/communities</text> 
        <input type="radio" name="mode" value="true" @if (ViewBag.DashboardSingleCourseMode) {<text>checked="checked"</text>}/>
            <text>Show only <em>@Helpers.CourseTag(ViewBag.ActiveCourse.AbstractCourse)</em></text> 
    }
</div>
<script type="text/javascript">
    $("input[name='mode']").change(function () {
        $('#set_dashboard').submit();
    });
</script>
<div id="dp_posts">
    @foreach (DashboardPost dp in ViewBag.DashboardPosts)
    {
        @Html.Partial("_DashboardPost", dp);
    }
</div>


@* TODO: Make this into a general purpose pagination helper? *@
@if (ViewBag.StartPost > 0)
{
    int newStart = Math.Max(ViewBag.StartPost - ViewBag.PostsPerPage, 0);

    @Html.ActionLink("<< Newest Posts", "Index", new { startPost = 0 })

    if (newStart != 0)
    {
        <text>&nbsp;&nbsp;</text>
        @Html.ActionLink("< Newer Posts", "Index", new { startPost = Math.Max(ViewBag.StartPost - ViewBag.PostsPerPage, 0) })
    }
}

@if ((ViewBag.StartPost + ViewBag.PostsPerPage) < ViewBag.DashboardPostCount)
{
    int last;
    if (ViewBag.DashboardPostCount % ViewBag.PostsPerPage == 0)
    {
        last = ViewBag.DashboardPostCount - ViewBag.PostsPerPage;
    }
    else
    {
        last = ViewBag.DashboardPostCount - ViewBag.DashboardPostCount % ViewBag.PostsPerPage;
    }

    int newEnd = Math.Max(last, ViewBag.StartPost + ViewBag.PostsPerPage);

    int older = ViewBag.StartPost + ViewBag.PostsPerPage;

    if (older < newEnd)
    {
<text>&nbsp;&nbsp;</text>
@Html.ActionLink("Older Posts >", "Index", new { startPost = older })
    }
<text>&nbsp;&nbsp;</text>
@Html.ActionLink("Oldest Posts >>", "Index", new { startPost = newEnd })

}